package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.CustomerFootprint;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.CustomerFootprintDao;
import com.weipai.springboot.module.service.CustomerFootprintService;
import com.weipai.springboot.module.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by ${lyh} on 2018/8/28.
 */

@Service
public class CustomerFootprintServiceImpl implements CustomerFootprintService{


    @Autowired
    CustomerFootprintDao customerFootprintDao;

    @Override
    public Response selectAllByCustomerFootprint(String customerId, Integer page, Integer size) {
        page = (page-1)*size;
        List<Map<String, Object>> list = customerFootprintDao.selectAllByCustomerFootprint(customerId,page,size);
        Response response = new Response();
        if(list.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.put("list", list);
        }else{
            response.setCode(RetCode.NO_DATA);
        }

        return response;
    }

    @Override
    public Response saveGoods(String customerId, String goodsId) {
        Response response = new Response();
        CustomerFootprint customerFootprint = new CustomerFootprint();
        List<CustomerFootprint> customerFootprints = customerFootprintDao.verCustomerFootprint(customerId,goodsId);
        if(customerFootprints.size()>0){
            response.setCode(RetCode.REPEAT_DATA);
        }else {
            customerFootprint.setFootprintId( UUID.randomUUID().toString().replaceAll("-", ""));
            customerFootprint.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
            customerFootprint.setCustomerId(customerId);
            customerFootprint.setGoodsId(goodsId);
            if(1==customerFootprintDao.insert(customerFootprint)){
                response.setCode(RetCode.SUCCESS);
            }else {
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }
        return response;
    }


    @Override
    public Response deleteById(String footprintId) {
        Response response = new Response();
        if(1==customerFootprintDao.deleteByPrimaryKey(footprintId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response allDel(String[] footprintId) {
        Response response = new Response();
         int rr = 0;
        if(footprintId.length>0) {
            for (String id : footprintId) {
                rr = customerFootprintDao.deleteByPrimaryKey(id);
            }
        }else{
            rr = customerFootprintDao.deleteByPrimaryKey(footprintId.toString());
        }
        if(rr>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response deleteByCustomer(String customerId) {
        Response response = new Response();
        if(customerFootprintDao.deleteByCustomer(customerId)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

}
